/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package database;

import java.io.IOException;
import java.sql.*;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.Artista;
import model.ArtistaDAO;
import model.ContaDAO;
import model.Usuario;
import model.UsuarioDAO;
import util.PropertiesManager;

/**
 *
 * @author Karen
 */
public class DBConnection {

    private final String USER;
    private final String SENHA;
    private final String URL;
    private final String DRIVER;
    private Connection conexao;

    public DBConnection() {
        USER = buscaBanco("user");
        SENHA = buscaBanco("senha");
        URL = buscaBanco("url");
        DRIVER = buscaBanco("driver");
    }

    public boolean openConnection() throws ClassNotFoundException, SQLException {
        boolean status = false;
        Class driver = Class.forName(DRIVER); //registra o DRIVER
        conexao = DriverManager.getConnection(URL, USER, SENHA);
        if (conexao != null) {
            status = true;
        }
        return status;
    }

    public void closeConnection() throws SQLException {
        if (conexao != null) {
            conexao.close();
            conexao = null;
        }
    }

    public static void main(String[] args) {
        /*DBConnection conect = new DBConnection();
        boolean teste;
        try {
            teste = conect.openConnection();
            if (teste) System.out.println("Abriu");
            conect.closeConnection();
            System.out.println("Fechou");
        } catch (ClassNotFoundException ex) {
            System.out.println(ex);
        } catch (SQLException ex) {
            System.out.println(ex);
        }*/
         try {
            UsuarioDAO udao = new UsuarioDAO();  
            ContaDAO cdao = new ContaDAO();
            ArtistaDAO adao = new ArtistaDAO();
            
            Usuario u = new Usuario(0, "8000", "8008", "Nappa", 0, "M", "umcaramauquegostademachucaraspessoas@gmail.com", "oque");
            Artista a = new Artista(0, "The Vegetas", "Uma Banda Super Sayajin", u);
            
            //System.out.println(udao.pesquisarPorEmail("fracassado@gmail.com"));                        
            //udao.inserir(u);
            
            u = udao.pesquisarPorEmail("umcaramauquegostademachucaraspessoas@gmail.com");
            //u.getContaUsuario().creditar(800);
            //cdao.editar(u);
            //adao.inserir(artista);
            a = adao.pesquisarPorUsuario(u);
            
            a.setDescricaoLivre("MODAFAKA");
            a.setNomeArtistico("Sith Loren");
            adao.editar(a);
            
            //udao.editar(u);
            //System.out.println(u);
            //System.out.println(udao.pesquisarPorEmail("m"));
            //System.out.println(udao.pesquisarTudo());
            System.out.println("FIM");
            
        } catch (ClassNotFoundException ex) {
            System.out.println(ex);
        } catch (SQLException ex) {
            System.out.println(ex);
        }        
    }
    
    public Connection getConexao() {
        return conexao;
    }

    private String buscaBanco(String chave) {
        String sql = null;
        try {
            PropertiesManager pm = new PropertiesManager("db.properties");
            HashMap dados = pm.readPropertiesFile();
            sql = (String) dados.get(chave);
        } catch (IOException ex) {
            Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE, null, ex);
        }
        return sql;
    }
}
